function initCheckboxGroup() {
    const eles = document.querySelectorAll('.gd-checkbox-group')
    eles.forEach(ele => {
        const isSingle = ele.hasAttribute('single')

        const gdCheckboxs = ele.querySelectorAll('.gd-checkbox')
        gdCheckboxs.forEach(gdCheckbox => {
            gdCheckbox.addEventListener('click', e => {
                if(e.target.tagName !== 'INPUT') {
                    const input = gdCheckbox.querySelector('input[type="checkbox"]')
                    input.checked = !input.checked
                }
                if(isSingle) {
                    let otherEle = []
                    // 获取所有兄弟节点
                    const all = gdCheckbox.parentNode.children
                    for (let i = 0 ; i<all.length;i++) {
                        if(all[i] !== gdCheckbox) {
                            otherEle.push(all[i])
                        }
                    }
                    otherEle.forEach(otherCheckbox => {
                        const otherInput = otherCheckbox.querySelector('input[type="checkbox"]')
                        otherInput.checked = false
                    })
                }
            })
        })
    })
}
class CheckboxGroup {
    constructor(element) {
        this.element = element || document.querySelector('.gd-checkbox-group')
        this.checkedEvent = null
        this.beforeCheckEvent = null
    }
    init() {
        const isSingle = this.element.hasAttribute('single')

        const gdCheckboxs = this.element.querySelectorAll('.gd-checkbox')
        gdCheckboxs.forEach((gdCheckbox,i) => {
            gdCheckbox.addEventListener('click', e => {
                this.beforeCheckEvent && this.beforeCheckEvent()

                if(e.target.tagName !== 'INPUT') {
                    const input = gdCheckbox.querySelector('input[type="checkbox"]')
                    input.checked = !input.checked
                }
                if(isSingle) {
                    let otherEle = []
                    // 获取所有兄弟节点
                    const all = gdCheckbox.parentNode.children
                    for (let i = 0 ; i<all.length;i++) {
                        if(all[i] !== gdCheckbox) {
                            otherEle.push(all[i])
                        }
                    }
                    otherEle.forEach(otherCheckbox => {
                        const otherInput = otherCheckbox.querySelector('input[type="checkbox"]')
                        otherInput.checked = false
                    })
                }
                this.checkedEvent && this.checkedEvent(gdCheckbox, i)
            })
        })
    }
    getAllCheckboxGroup () {
        this.elements = document.querySelectorAll('.gd-checkbox-group')
    }
    setBeforeCheckEvent(fn) {
        this.beforeCheckEvent = fn
    }
    setCheckedEvent(fn) {
        this.checkedEvent = fn
    }
}
export {
    CheckboxGroup,
    initCheckboxGroup
}